import { defineStore } from 'pinia'
import { $utils } from '@utils'
import { useGameStore } from './game'
import { getLotteryTemplate } from '@/api'

const playMapComponents = {
  0: 'jndebTmGame',
  1: 'jndebTxGame',
  2: 'jndebTdGame', //
  3: 'jndebTwGame', //尾项
}

export const useQxcStore = defineStore('pcnn', {
  state: () => ({
    gameTemplateData: [], //游戏渲染数据
    gameComponentTag: 'jndebTmzPlay', // 游戏组件标签
    currentGameType: 0, //当前游戏索引
    gameStore: useGameStore(),
  }),
  getters: {
    // 当前选中游戏组件
    getGameComponentTag(state) {
      return playMapComponents[state.currentGameType]
    },
    // 获得 选中游戏类型数据
    getCurrentGameData() {
      if ($utils.isEmpty(this.gameTemplateData)) return []
      return this.gameTemplateData[this.currentGameType]
    },
  },
  actions: {
    // 入口，初始化
    async init() {
      this.commit('jndeb/fetchGameTemplateData')
    },
    // 当前游戏索引
    setCurrentGameType(state, index) {
      state.currentGameType = index
    },

    // 立即下注
    betNow(refs) {
      refs['gameRef'].betNow()
    },

    // 清空所有下注内容
    clearInvestList(refs) {
      refs['gameRef'].updateInvestList('clear')
    },

    // 删除一个游戏的下注订单
    deleteAnInvest(data) {
      data.dom['gameRef'].updateInvestList('del', data.data)
    },

    // 撤消订单
    withdrawAnOrder() {},

    // 获得游戏渲染数据
    async fetchGameTemplateData(state) {
      const res = await getLotteryTemplate({ lotteryCode: 'QXC', handicapCode: this.gameStore.currentHandicapCode })
      state.gameTemplateData = res.data
    },
  },
})
